package com.frogs.ucenter.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.jsoup.Connection.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.web.api.entity.ApiUrl;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.system.web.api.response.Response.ResultParamEnum;
import com.frogs.ucenter.service.AbsService;
import com.frogs.ucenter.service.TagService;

@Service("tagService")
public class TagServiceImpl extends AbsService implements TagService {
	
	private static Logger log = LoggerFactory.getLogger(TagServiceImpl.class);

	@Override
	public JSONObject getTagListBySexaAge(Integer sex, Integer age) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("TAG_LIST");
		String[] otherParams = {
					ApiUrlParamEnum.sex.getParamName(),sex.toString(),
					ApiUrlParamEnum.age.getParamName(),age.toString(),
					
				};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (null == result || !result.containsKey("response") 
					|| ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
		} catch (Exception e) {
			log.error("func[getTagListBySexaAge] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}
	
	@Override
	public List<Map<String, String>> getUserTags(int userId) {
		JSONObject tagJson = getUserTagsForJson(userId);
		
		return null;
	}

	@Override
	public JSONObject getUserTagsForJson(int userId) {
		return null;
	}

	@Override
	public JSONObject deleteUserTag(Integer userId, Integer tagId)
			throws Exception {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_TAG_DELETE");
		JSONObject result = null;
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(), userId.toString(),
				ApiUrlParamEnum.tagId.getParamName(), tagId.toString(),
				};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
			return result;
		} catch (IllegalStateException e) {
			log.error("func[deleteUserTag] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),"", e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}catch(Exception e){
			throw new Exception("删除标签接口异常！");
		}
		return null;
	}
	
	@Override
	public JSONObject relateUserTag(Integer userId, Integer tagId)
			throws Exception {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_TAG_RELATE");
		JSONObject result = null;
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(), userId.toString(),
				ApiUrlParamEnum.tagId.getParamName(), tagId.toString(),
				};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			return result;
		} catch (IllegalStateException e) {
			log.error("func[deleteUserTag] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),"", e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}catch(Exception e){
			throw new Exception("添加标签接口异常！");
		}
		return null;
	}



}
